//index.js
Page({  
  data: {
    // 主图
    logo_image: '../../utils/taijia_logo.png',
    logo_image: '',
    // 地址
    address: '',
    // 营业时间    
    open: '',
    // 联系方式
    contact: '',  
    // 照片展示  
    show_images: [],
    // 展示 视频
    show_videos: [],
    // 坐标信息
    location: {
      lat: '',
      lng: ''
    },
  },
  //事件处理函数
  getData: function() {
    var self = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {  
        var lat = res.latitude;
        var lng = res.longitude;
        wx.request({ 
          url: 'https://api.hgdqdev.cn/yaodian/yaodian',  
          // url: 'http://localhost:3000/yaodian/yaodian',
          data: {
            lat: lat,
            lng: lng
          },
          success: function (res) {
            if(res.data.status == 100){
              var shop_info = res.data.shop;
              var open = shop_info.open_time_start + '-' + shop_info.open_time_end;
              var lna = {
                lat: Number(shop_info.lat),
                lng: Number(shop_info.lng)
              }
              var show_images = res.data.show_images;
              var show_videos = res.data.show_videos;  
              var logo_image = res.data.images;
              var show_address = shop_info.province + shop_info.city + shop_info.sarea + shop_info.address;
              self.setData({
                logo_image: logo_image,
                address: show_address,
                open: open,
                contact: shop_info.contact,
                show_images: show_images,
                show_videos: show_videos,
                location: lna
              })
            }else{
              wx.showModal({
                title: '请求失败',
                content: '请求失败,请稍后重试',
                success: function (res) {
                  if (res.confirm) {
                    // console.log('用户点击确定')
                  } else if (res.cancel) {
                    // console.log('用户点击取消')
                  }
                }
              })
            }
          },
          fail: function () {
            wx.showToast({
              title: '请求失败,请稍后重试!',
              icon: 'loading',
              duration: 2000
            })
          }
        })
      }
    }) 
  },
  imageTap: function(e){
    var index = e.currentTarget.dataset.id;
    var currentSrc = this.data.show_images[index];
    wx.previewImage({
      current: currentSrc, // 当前显示图片的http链接
      urls: this.data.show_images // 需要预览的图片http链接列表
    })
  },
  mapTap: function(){
    var locl = this.data.location;
    wx.openLocation({
      latitude: locl.lat,
      longitude: locl.lng,  
      scale: 28
    })
  },
  onLoad: function () {  
  },
  onShow: function(){
    this.getData();    
  }  
})
